﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;

namespace IntraVision.Web
{
    public class LanguageController : ControllerBase
    {
        public virtual void Change(string lang)
        {
            Change(lang, "~/");
        }

        public void Change(string lang,string returnURL)
        {
            var culture = CultureInfo.CreateSpecificCulture(lang);
            Session["Language"] = lang;
            string cookieKey = ConfigurationManager.AppSettings["languageCookieKey"];
            
            if (string.IsNullOrEmpty(cookieKey)) cookieKey = String.Format(CultureInfo.InvariantCulture, "LANG_{0}", Request.Url.Host);
            HttpCookie cookie = new HttpCookie(cookieKey);

            cookie.Values.Add("lang", Server.UrlEncode(lang));
            cookie.Expires = DateTime.Now.AddYears(1);

            Response.SetCookie(cookie);
            Response.Redirect(returnURL);
        }
    }
}
